using System;
using UnityEngine;
using Voodoo.Sauce.Internal.Analytics;
using Voodoo.Sauce.Internal.CrossPromo.Models;
using Voodoo.Sauce.Internal.VoodooTune;

namespace Voodoo.Sauce.Internal.CrossPromo.Configuration
{
	internal class CrossPromoConfigurationHelper
	{
		internal enum ShouldBeShownReason
		{
			CanBeShown = 0,
			SessionTooEarly = 1,
			SessionCooldown = 2,
			GameTooEarly = 3,
			GameCooldown = 4,
			TooManyImpressions = 5,
			NotEnabled = 6
		}

		private CrossPromoConfiguration _configuration;

		private int _impressionsCount;

		private int _lastSessionCountWhenShown = -1;

		private int _lastGamePlayedWhenShown;

		private int _gamesPlayed;

		private bool _crossPromoEnabled;

		private int _currentSessionCooldown;

		private const string K_PREFS_LAST_SESSION_SHOWN = "CrossPromo_LastSessionShown";

		private int LastSessionCountWhenShown
		{
			get
			{
				if (_lastSessionCountWhenShown == -1)
				{
					_lastSessionCountWhenShown = NintendoPlayerPrefs.GetInt("CrossPromo_LastSessionShown", 0);
				}
				return _lastSessionCountWhenShown;
			}
			set
			{
				_lastSessionCountWhenShown = value;
				NintendoPlayerPrefs.SetInt("CrossPromo_LastSessionShown", _lastSessionCountWhenShown);
			}
		}

		private int SessionCount => AnalyticsSessionManager.Instance().SessionInfo.count;

		private int GameCount => AnalyticsStorageHelper.Instance.GetGameCount();

		internal void Initialize(CrossPromoConfiguration configuration, bool crossPromoEnabled)
		{
			_configuration = configuration ?? new CrossPromoConfiguration();
			_crossPromoEnabled = crossPromoEnabled;
			_impressionsCount = 0;
			_gamesPlayed = 0;
			_lastGamePlayedWhenShown = 0;
			_currentSessionCooldown = SessionCount - LastSessionCountWhenShown;
			if (ShouldBeEnabled())
			{
				CrossPromoEvents.OnShown += OnShown;
				CrossPromoEvents.OnClick += OnClick;
			}
		}

		internal bool ShouldBeEnabled()
		{
			if (_crossPromoEnabled)
			{
				return _configuration.enabled;
			}
			return false;
		}

		internal ShouldBeShownReason GetShouldBeShownReason()
		{
			return GetShouldBeShownReason(SessionCount, _currentSessionCooldown, _gamesPlayed, _lastGamePlayedWhenShown, _impressionsCount, LastSessionCountWhenShown, _lastGamePlayedWhenShown);
		}

		internal ShouldBeShownReason GetShouldBeShownReason(int sessionsCount, int sessionsCooldown, int gamesPlayed, int lastGamesPlayed, int impressionsCount, int lastSessionCountWhenShown = 0, int lastGamePlayedWhenShown = 0)
		{
			if (!ShouldBeEnabled())
			{
				return ShouldBeShownReason.NotEnabled;
			}
			if (_configuration.impressionsCountBeforeHiding > 0 && impressionsCount > _configuration.impressionsCountBeforeHiding)
			{
				return ShouldBeShownReason.TooManyImpressions;
			}
			if (lastSessionCountWhenShown == 0 && sessionsCount < _configuration.sessionsCountBeforeShowing)
			{
				return ShouldBeShownReason.SessionTooEarly;
			}
			if (sessionsCooldown < _configuration.sessionsCountBetweenShowing)
			{
				return ShouldBeShownReason.SessionCooldown;
			}
			if (lastGamePlayedWhenShown == 0 && gamesPlayed < _configuration.gamesCountBeforeShowing)
			{
				return ShouldBeShownReason.GameTooEarly;
			}
			if (Mathf.Abs(gamesPlayed - lastGamesPlayed) < _configuration.gamesCountBetweenShowing)
			{
				return ShouldBeShownReason.GameCooldown;
			}
			return ShouldBeShownReason.CanBeShown;
		}

		internal bool ShouldBeShown()
		{
			return GetShouldBeShownReason() == ShouldBeShownReason.CanBeShown;
		}

		internal string GetGamesToShow()
		{
			if (!ShouldBeEnabled())
			{
				return "";
			}
			return _configuration.gamesToShow.Replace(" ", "");
		}

		private void OnClick(AssetModel obj)
		{
			_impressionsCount = 0;
		}

		private void OnShown(AssetModel obj)
		{
			LastSessionCountWhenShown = SessionCount;
			_lastGamePlayedWhenShown = _gamesPlayed;
			_impressionsCount++;
		}

		private void OnGamePlayed(int level, bool victory)
		{
			_gamesPlayed++;
		}

		internal string GetCurrentStateDebugText()
		{
			int sessionCount = SessionCount;
			int gameCount = GameCount;
			int impressionsCount = _impressionsCount;
			int num = _gamesPlayed - _lastGamePlayedWhenShown;
			return string.Concat(string.Concat(string.Concat($"session: {sessionCount} - games: {gameCount} - imps: {impressionsCount}" + Environment.NewLine, $"sessionCooldown: {_currentSessionCooldown} - gameCooldown: {num}", Environment.NewLine), $"enabled: {ShouldBeEnabled()} - waterfallId: {GetWaterfallId()}", Environment.NewLine), $"Shown Reason: {GetShouldBeShownReason()}", Environment.NewLine);
		}

		internal string GetCurrentConfigurationDebugText()
		{
			return _configuration?.ToString() ?? "";
		}

		internal string GetWaterfallId()
		{
			if (!string.IsNullOrEmpty(_configuration?.waterfallId))
			{
				return _configuration.waterfallId;
			}
			if (!string.IsNullOrEmpty(VoodooTuneManager.GetMainAbTestCohortUuid()))
			{
				return VoodooTuneManager.GetMainAbTestCohortUuid();
			}
			return "no_waterfall";
		}
	}
}
